﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTCementStrength : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        {
            RPTCementStrengthService RPTCementStrengthServiceObj = ServiceFactory.getRPTCementStrengthService();
            OperationResult<List<tb_RPT_CementStrength>> result = RPTCementStrengthServiceObj.GetAllCementStrength();

            if (result.operationSuccess)
            {
                gvCementStrengthList.DataSource = result.obj;
                gvCementStrengthList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTCementStrengthService RPTCementStrengthServiceObj = ServiceFactory.getRPTCementStrengthService();
            OperationResult<tb_RPT_CementStrength> resultEdit = RPTCementStrengthServiceObj.GetCementStrengthById(id);

            if (resultEdit.operationSuccess == true)
            {
                txtEditTestDate.Text = resultEdit.obj.TestDate;
                txtEditCementManufacturer.Text = resultEdit.obj.CementManufacturer;
                txtEditCementLabel.Text = resultEdit.obj.CementLabel;
                txtEditAshManufacturer.Text = resultEdit.obj.AshManufacturer;
                txtEditQuickSide.Text = resultEdit.obj.QuickSide;
                txtEdit3DStrength.Text = resultEdit.obj._3DStrength;
                txtEdit7DStrength.Text = resultEdit.obj._7DStrength;
                txtEdit7DIndex.Text = resultEdit.obj._7DIndex;
                txtEdit28DStrength.Text = resultEdit.obj._28DStrength;
                txtEdit28DIndex.Text = resultEdit.obj._28DIndex;
                txtEditStatistician.Text = resultEdit.obj.Statistician;
                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTCementStrengthService RPTCementStrengthServiceObj = ServiceFactory.getRPTCementStrengthService();
            tb_RPT_CementStrength instance = new tb_RPT_CementStrength();

            instance.TestDate = txtTestDate.Text;
            instance.CementManufacturer = txtCementManufacturer.Text;
            instance.CementLabel = txtCementLabel.Text;
            instance.AshManufacturer = txtAshManufacturer.Text;
            instance.QuickSide = txtQuickSide.Text;
            instance._3DStrength = txt3DStrength.Text;
            instance._7DStrength = txt7DStrength.Text;
            instance._7DIndex = txt7DIndex.Text;
            instance._28DStrength = txt28DStrength.Text;
            instance._28DIndex = txt28DIndex.Text;
            instance.Statistician = txtStatistician.Text;

            OperationResult<tb_RPT_CementStrength> result = RPTCementStrengthServiceObj.CreateCementStrength(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTCementStrengthService RPTCementStrengthServiceObj = ServiceFactory.getRPTCementStrengthService();
            tb_RPT_CementStrength entity = new tb_RPT_CementStrength();

            entity.Id = editId;
            entity.TestDate = txtEditTestDate.Text;
            entity.CementManufacturer = txtEditCementManufacturer.Text;
            entity.CementLabel = txtEditCementLabel.Text;
            entity.AshManufacturer = txtEditAshManufacturer.Text;
            entity.QuickSide = txtEditQuickSide.Text;
            entity._3DStrength = txtEdit3DStrength.Text;
            entity._7DStrength = txtEdit7DStrength.Text;
            entity._7DIndex = txtEdit7DIndex.Text;
            entity._28DStrength = txtEdit28DStrength.Text;
            entity._28DIndex = txtEdit28DIndex.Text;
            entity.Statistician = txtEditStatistician.Text;  

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_CementStrength> resultEdit = RPTCementStrengthServiceObj.ModifyCementStrength(entity); 
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTCementStrengthService RPTCementStrengthServiceObj = ServiceFactory.getRPTCementStrengthService();
            tb_RPT_CementStrength deleteEntity = new tb_RPT_CementStrength();

            deleteEntity.Id = deleteId;
            RPTCementStrengthServiceObj.DeleteCementStrength(deleteEntity);

            BindGridView();
        }

        protected void gvCementStrengthList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvCementStrengthList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCementStrengthList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
